package com.mars.push.core.channel.firm.handler;

import com.google.gson.Gson;
import com.mars.push.api.common.MessageInput;
import com.mars.push.core.channel.AbstractPushChannel;
import com.mars.push.core.channel.PushMessage;
import com.mars.push.core.channel.firm.config.MiConfig;
import com.mars.push.core.channel.firm.dto.FirmPushMessage;
import com.mars.push.core.dto.PushMessageType;
import com.xiaomi.xmpush.server.Constants;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.xiaomi.xmpush.server.Message.NOTIFY_TYPE_ALL;

/**
 * @Author King
 * @create 2020/5/25 11:15
 */
@Slf4j
@Component
public class MiPushChannel extends AbstractPushChannel {

    private final static int RETRIES = 3;

    @Autowired
    private MiConfig miConfig;


    @Override
    protected Object androidPush0(PushMessage pushMessage) {
        //这里要注意，这是正式-启动方式，支持IOS跟Android,Constants.useSandbox();这是测试-启动方式，不支持Android;建议申请app
        FirmPushMessage m = (FirmPushMessage) pushMessage;
        Constants.useOfficial();
        Sender sender = new Sender(miConfig.getAppSecret());
        Message res = null;
        String token = m.getDeviceToken();
        if (m.getPushMessageType() != PushMessageType.PUSH_MESSAGE_TYPE_NORMAL
                && m.getPushMessageType() != PushMessageType.PUSH_MESSAGE_TYPE_FRIEND_REQUEST) {
            //10 min
            long timeToLive = 600 * 1000;
            res = new Message.Builder()
                    .payload(new Gson().toJson(m))
                    .restrictedPackageName(m.getPackageName())
                    //透传
                    .passThrough(1)
                    .timeToLive(timeToLive)
                    .enableFlowControl(false)
                    .build();
        } else {
            //10 min
            long timeToLive = 600 * 1000;
            res = new Message.Builder()
                    .payload(new Gson().toJson(m))
                    .title("新消息提醒")
                    .description(m.getPushContent())
                    .notifyType(NOTIFY_TYPE_ALL)
                    .restrictedPackageName(m.getPackageName())
                    .passThrough(0)
                    .timeToLive(timeToLive)
                    .enableFlowControl(true)
                    .build();
        }
        Result result = null;
//        try {
//           // result = sender.send(res, token, RETRIES);
//        } catch (IOException e) {
//            log.error(e.getMessage(), e);
//        }

        log.info("Server response: MessageId: " + result.getMessageId()
                + " ErrorCode: " + result.getErrorCode().toString()
                + " Reason: " + result.getReason());
        return result;
    }

    @Override
    public PushMessage createMessage(MessageInput input) {
        FirmPushMessage message = new FirmPushMessage();
        BeanUtils.copyProperties(input, message);
        return message;
    }

    @Override
    protected void onSuccess(PushMessage message, Object result) {

    }

    @Override
    protected void onFail(PushMessage message, Object result) {

    }

    @Override
    protected void onException(PushMessage message, Object result) {

    }

    @Override
    protected Object allPush0(PushMessage pushMessage) {
        return null;
    }

    @Override
    protected Object iosPush0(PushMessage pushMessage) {
        return null;
    }


}
